package com.cysyz.web.modules.base.permission.tag;

import com.cysyz.web.modules.base.permission.service.PermissionService;
import com.cysyz.web.modules.base.support.ServletActionContext;
import com.cysyz.web.modules.base.support.SysConstants;
import com.cysyz.web.modules.base.user.domain.SysUser;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

/**
 * Created by cuiyong on 15/1/24.
 */
public class PermissionATag extends SimpleTagSupport{
    private String module;
    private String moduleFunction;
    private String target;
    private String href;
    private String id;
    private String title;
    private String cssClass;
    private PermissionService permissionService;

    public PermissionATag() {
        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getRequest().getServletContext());
        permissionService = (PermissionService) wac.getBean("permissionService");

    }

    @Override
    public void doTag() throws JspException, IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        SysUser user = (SysUser) request.getSession().getAttribute(SysConstants.CURRENT_LOGIN_SYSUSER);
        if (user != null) {
            if (permissionService.hasPermission(user, module, moduleFunction)) {
                JspWriter out = getJspContext().getOut();
                StringBuilder aBuilder = new StringBuilder();
                aBuilder.append("<a href=\"");
                aBuilder.append(href);
                aBuilder.append("\" ");
                aBuilder.append("class=\"");
                aBuilder.append(cssClass);
                aBuilder.append("\" ");
                aBuilder.append("id=\"");
                aBuilder.append(id);
                aBuilder.append("\"");

                aBuilder.append("> ");
                if (title != null) {
                    aBuilder.append(title);
                }
                aBuilder.append("</a>");

                out.print(aBuilder.toString());
            }
        }
    }





    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getModuleFunction() {
        return moduleFunction;
    }

    public void setModuleFunction(String moduleFunction) {
        this.moduleFunction = moduleFunction;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }

    public PermissionService getPermissionService() {
        return permissionService;
    }

    public void setPermissionService(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCssClass() {
        return cssClass;
    }

    public void setCssClass(String cssClass) {
        this.cssClass = cssClass;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
